package com.itbaizhan.openhis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itbaizhan.openhis.domain.Provider;
import com.itbaizhan.openhis.dto.ProviderDto;
import com.itbaizhan.openhis.service.ProviderService;
import com.itbaizhan.openhis.mapper.ProviderMapper;
import com.itbaizhan.openhis.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

/**
 *
 */
//@Service
@Service      //dubbo的service，进行远程调用
public class ProviderServiceImpl extends ServiceImpl<ProviderMapper, Provider>
    implements ProviderService{

    @Autowired
    private ProviderMapper providerMapper;

    @Override
    public DataGridView listProviderForPage(ProviderDto providerDto) {
        //用于测试sentinel的熔断
        try{
            Thread.sleep(500);
        }catch (Exception e){
            e.printStackTrace();
        }
        Page<Provider> page = new Page<>(providerDto.getPageNum(),providerDto.getPageSize());
        QueryWrapper<Provider> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(providerDto.getProviderName()),Provider.COL_PROVIDER_NAME,providerDto.getProviderName());
        wrapper.like(StringUtils.isNotBlank(providerDto.getContactName()),Provider.COL_CONTACT_NAME,providerDto.getContactName());
        //表中的手机联系人字段与联系电话，查到其中一个就可以
        wrapper.and(StringUtils.isNotBlank(providerDto.getContactTel()), new Consumer<QueryWrapper<Provider>>() {
            @Override
            public void accept(QueryWrapper<Provider> providerQueryWrapper) {
                providerQueryWrapper.like(Provider.COL_CONTACT_TEL,providerDto.getContactTel()).or()
                        .like(Provider.COL_CONTACT_MOBILE,providerDto.getContactMobile());
            }
        });
        wrapper.eq(StringUtils.isNotBlank(providerDto.getStatus()),Provider.COL_STATUS,providerDto.getStatus());
        providerMapper.selectPage(page,wrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public int addProvider(ProviderDto providerDto) {
        Provider provider = new Provider();
        BeanUtil.copyProperties(providerDto,provider);
        provider.setCreateBy(providerDto.getSimpleUser().getUserName());
        provider.setCreateTime(DateUtil.date());

        return providerMapper.insert(provider);
    }

    @Override
    public int updateProvider(ProviderDto providerDto) {
        Provider provider = new Provider();
        BeanUtil.copyProperties(providerDto,provider);
        provider.setUpdateBy(providerDto.getSimpleUser().getUserName());

        return providerMapper.updateById(provider);
    }

    @Override
    public Provider getOne(Long providerId) {
        return providerMapper.selectById(providerId);
    }

    @Override
    public int deleteProviderByIds(Long[] providerIds) {
        List<Long> ids = Arrays.asList(providerIds);
        if(ids != null && ids.size() >0){
            return providerMapper.deleteBatchIds(ids);
        }
        return 0;
    }
}




